/*
** zipcode check 邮政校验
**
**
*/

(function() {
	'use strict';

	angular
		.module('elife')
		.directive('zipcodeCheck', zipcodeCheck);

	zipcodeCheck.$inject = ['VALIDATION'];
	/** @ngInject */
	function zipcodeCheck(VALIDATION) {
		var directive = {
			restrict: 'A',
			require: 'ngModel',
			link: linkFunc
		};

		return directive;

		function linkFunc(scope, elem, attr, ngModel) {
			scope.$watch(attr.ngModel, function(value) {
				if (!value) {
					ngModel.$setValidity('invalid', true);
					ngModel.$setValidity('length', true);
					return;
				}

				if (!VALIDATION.zipcodeCheck(value)) {
					// 非法字符校验
					ngModel.$setValidity('invalid', false);
				} else {
					ngModel.$setValidity('invalid', true);
				}
			});
		}
	}

})();
